package com.example.practice;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private Button lSetting;
    private EditText lUser;
    private EditText lPassword;
    private Button LRt;
    private Button LPost;
    private SqliteUsers sUsers;
    private SQLiteDatabase db;
    private EditText lPasswords;
    private EditText lPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();

    }

    private void initView() {
        lSetting = (Button) findViewById(R.id.l_setting);
        lUser = (EditText) findViewById(R.id.l_user);
        lPassword = (EditText) findViewById(R.id.l_password);
        LRt = (Button) findViewById(R.id.L_rt);
        LPost = (Button) findViewById(R.id.L_post);
        lSetting.setOnClickListener(this);
        LRt.setOnClickListener(this);
        LPost.setOnClickListener(this);
        lPasswords = (EditText) findViewById(R.id.l_passwords);
        lPasswords.setOnClickListener(this);
        lPhone = (EditText) findViewById(R.id.l_phone);
        lPhone.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.l_setting:
                startActivity(new Intent(LoginActivity.this,SettingActivity.class));
                break;
            case R.id.L_rt:
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                break;
            case R.id.L_post:
                submit();
                break;
        }
    }

    private void submit() {
        sUsers = new SqliteUsers(LoginActivity.this);
        db = sUsers.getReadableDatabase();
      ContentValues values = new ContentValues();
        String passwords = lPasswords.getText().toString().trim();
        String phone = lPhone.getText().toString().trim();
        String user = lUser.getText().toString().trim();
        String password = lPassword.getText().toString().trim();
        if (!TextUtils.isEmpty(user)) {
            values.put("info_user", user);
        }
        if (!TextUtils.isEmpty(password)&&password.equals(passwords)) {
            values.put("info_password", password);
        }else {
            Toast.makeText(this, "密码不一致", Toast.LENGTH_SHORT).show();
        }
        if (TextUtils.isEmpty(phone)&&tool(phone)) {
                values.put("info_phone",phone);
            }else {
                Toast.makeText(this, "手机号码错误", Toast.LENGTH_SHORT).show();
            }
        if(tool(phone)&&password.equals(passwords)){
        long info = db.insert("info", null, values);
        String s ;
        if (info == -1) {
            s = "注册失败";
        } else {
            s = "注册成功";
        }
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
        }
    }
    private Boolean tool(String mobiles){
        String telRegex = "[1][3578]\\d{9}";//"[1]"代表第1位为数字1，"[3578]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        if (TextUtils.isEmpty(mobiles)){
            return false;
        }
        else return mobiles.matches(telRegex);
    }
}
